<?php

namespace addons\aws\controller;

use app\common\exception\UploadException;
use app\common\library\Upload;
use Aws\AwsService;
use think\addons\Controller;
use think\console\command\Help;
use think\Env;
class Index extends Controller
{

    public function index()
    {
        $this->error("当前插件暂无前台页面");
    }

    public function upload(){
        //默认普通上传文件
        $file = $this->request->file('file');
        $name = $this->request->post('name');
//        try {
            $upload = new Upload($file);
            //文件绝对路径

            $position = strrpos($name, '.');
            if ($position !== false) {
                $ext = substr($name, $position + 1);
            }
            $path = $upload->getFile()->getPathname();
            $info = AwsService::make()->uploadByPath($path,$ext);
            if($info) {
                $url = $info['ObjectURL'];

                $url = str_replace(AwsService::$endpoint.'/'.AwsService::$bucket1,AwsService::$aws_url1,$url);
                $r = [
                    'code' => 1,
                    'msg' => "上传成功",
                    "data" => [
                        "url" => $url,
                        "fullurl" => $url
                    ],
                    "url" => "",
                    "wait" => 3
                ];
            }else{
                $r = [
                    'code' => -100,
                    'msg' => "上传失败",
                    "data" => [],
                    "url" => "",
                    "wait" => 3
                ];
            }
         //    echo '{"code":1,"msg":"上传成功","data":{"url":"https:\/\/bs.kjcdn.com\/i\/pubwmrsn\/vo\/go\/o2g9v.jpg",
            //"fullurl":"https:\/\/bs.kjcdn.com\/i\/pubwmrsn\/vo\/go\/o2g9v.jpg"},"url":"","wait":3}';
             return json($r);
//            die;
//            if ($url) {
//                //echo '{"code":0,"msg":"上传成功","data":"","url":"'.$url.'","wait":3}';
               // $this->success("上传成功", '', ['url' => $url, 'fullurl' => $url]);
//            } else {
//                $this->error("上传失败");
//            }
//        } catch (UploadException $e) {
//            $this->error($e->getMessage());
//        }

    }
    public function uploadJob($path){
        //默认普通上传文件
//        try {
        $ext = pathinfo($path, PATHINFO_EXTENSION);
        $info = AwsService::make()->uploadByPath($path,$ext);
        if($info) {
        $url = $info['ObjectURL'];
        $url = str_replace(Env::get('S3.AWS_BUCKET').'.'.Env::get('S3.AWS_ENDPOINT'), Env::get('S3.AWS_URL'), $url);
        return $url;
        }else{
            return false;
        }
//            die;
//            if ($url) {
//                //echo '{"code":0,"msg":"上传成功","data":"","url":"'.$url.'","wait":3}';
        // $this->success("上传成功", '', ['url' => $url, 'fullurl' => $url]);
//            } else {
//                $this->error("上传失败");
//            }
//        } catch (UploadException $e) {
//            $this->error($e->getMessage());
//        }

    }
}
